package com.sunny.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.sunny.common.Const;
import com.sunny.dao.AttentionsMapper;
import com.sunny.dao.HouseMapper;
import com.sunny.pojo.Attentions;
import com.sunny.pojo.House;
import com.sunny.pojo.SelectHouseCondition;
import com.sunny.service.IHouseService;
import com.sunny.utils.PageInformation;
import com.sunny.utils.PageUtil;
import com.sunny.utils.ServerResponse;
import com.sunny.vo.HouseVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName:
 * @Description:
 * @author: yubao.jin
 * @date: 2018/5/13
 * @version:V1.0
 */
@Service("iHouseService")
public class HouseServiceImpl implements IHouseService {
    @Autowired
    HouseMapper houseMapper;

    @Autowired
    AttentionsMapper attentionsMapper;

    @Override
    public ServerResponse add(House house) {
        int resoultCount = houseMapper.insertSelective(house);
        if(resoultCount == 0) {
            return ServerResponse.createByErrorMessage("新增失败");
        }

        return ServerResponse.createBySuccessMessage("新增成功");
    }

    @Override
    public ServerResponse del(String[] id) {
        // 校验参数
        if(id != null  && id.length==0) {
            return ServerResponse.createByErrorMessage("您未选择，请重新选择");
        }

        // 数组转化为list
        List<String> idList = Arrays.asList(id);
        int resultCount = houseMapper.deleteByIds(idList);
        // 检验
        if(resultCount == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        }

        return ServerResponse.createBySuccessMessage("删除成功");
    }

    @Override
    public PageInformation<House> list(PageUtil page, House house) {
        // 利用pageHelper进行分页查询
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<House> list = houseMapper.selectHouseByHouse(house);
        PageInfo<House> pageInfo = new PageInfo(list);

        // 组装PageInformation返回前端数据
        PageInformation<House> pageInformation =  PageInformation.getInstance();
        pageInformation.setRows(pageInfo.getList());
        pageInformation.setTotal(pageInfo.getTotal());

        return pageInformation;
    }

    @Override
    public PageInformation<HouseVo> customerList(PageUtil page, SelectHouseCondition house, Integer UserId) {
        // 利用pageHelper进行分页查询
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<House> list = houseMapper.selectHouseBySelectHouseCondition(house);
        PageInfo<House> pageInfo = new PageInfo(list);

        List<House> listHouse = pageInfo.getList();
        List<HouseVo> houseVoList = Lists.newArrayList();

        if(CollectionUtils.isNotEmpty(listHouse)) {
            for(House h : listHouse){
                Attentions attention = new Attentions();
                attention.setBuyerid(UserId);
                attention.setHouseid(h.getId());
                Attentions atten = attentionsMapper.selectByAttentionsByAttention(attention);
                if(atten != null) {
                    h.setIsattention(1);
                } else {
                    h.setIsattention(0);
                }
            }
        }

        // 组装视图
        if(CollectionUtils.isNotEmpty(listHouse)) {
            int toIndex = 3;
            int size = listHouse.size();

            for(int i = 0;i < size;i+=3){
                if((i+3) > size){        //作用为toIndex最后没有3条数据则剩余几条newList中就装几条
                    toIndex = size - i;
                }
                List<House> newList = list.subList(i,i+toIndex);
                HouseVo houseVo = new HouseVo();
                for(int j = 0; j < newList.size(); ++j) {
                    switch (j) {
                        case 0 : houseVo.setHouseOne(newList.get(0)); break;
                        case 1 : houseVo.setHouseTwo(newList.get(1)); break;
                        case 2 : houseVo.setHouseThree(newList.get(2)); break;
                    }
                }
                houseVoList.add(houseVo);
            }
        }

        // 组装PageInformation返回前端数据
        PageInformation<HouseVo> pageInformation =  PageInformation.getInstance();
        pageInformation.setRows(houseVoList);
        pageInformation.setTotal(getTotal(pageInfo.getTotal(), 3.0));

        return pageInformation;
    }

    @Override
    public ServerResponse search(House house) {
        House hou = houseMapper.selectByPrimaryKey(house.getId());
        if(hou == null) {
            return ServerResponse.createByErrorMessage("查询失败");
        }

        return ServerResponse.createBySuccess("查询成功", hou);
    }

    private Integer getTotal(long total, Double rate) {
        Double totalOne = total / rate;
        Integer totalTwo = totalOne.intValue();
        if(totalTwo < totalOne) {
            return totalTwo + 1;
        }

        return totalTwo;
    }

    @Override
    public ServerResponse passOrNoPass(House house) {
        int count = houseMapper.passHouseByHouse(house);
        if(count == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        }

        return ServerResponse.createBySuccessMessage("修改成功");
    }
}
